package com.glinsoft.ticktak.controller.support;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.glinsoft.ticktak.controller.common.JSON;
import java.util.ArrayList;
import java.util.List;

/**
 * 异常处理
 */
@RestControllerAdvice
public class ErrorOccurHandler {

    @ExceptionHandler(Exception.class)
    public JSON exceptionHandler(Exception e){
        List<String> stackTraces = new ArrayList();
        StringBuilder sb = new StringBuilder();
        stackTraces.add(e.getMessage());
        sb.append(e.getMessage());
        sb.append("\n");
        stackTraces.add("StackTrace: ");
        String errorMsg;
        for(StackTraceElement el : e.getStackTrace()){
            errorMsg = el.toString();
            stackTraces.add(errorMsg);
            sb.append(errorMsg);
            sb.append("\n");
        }
        System.err.println(sb.toString());
        return JSON.DATA(-4,"运行期异常",stackTraces);
    }

}
